import RevisionItem from '../stores/ckextra/RevisionItem';

const isText = (node) => {
  return node instanceof CKEDITOR.dom.text;
}
const isElement = (node) => {
  return node instanceof CKEDITOR.dom.element;
}
const isNode = (node) => {
  return node instanceof CKEDITOR.dom.node;
}
const isRoot = (node) => {
  return (isElement(node) && node.getName() === 'body') ||
    (isElement(node) && node.getName() === 'html');
}
const paragraphEleMap = {'p':1, 'div':1, 'li':1, 'ul': 1};
const isParagraph = (node) => {
  if (isElement(node) && node.getName() in paragraphEleMap) {
    return true;
  }
  return false;
}


const clearEmptyChar= (input) => {
  return input.replace(/[\u200B-\u200D\uFEFF]/g, '');
}

const util = {
  getEditor: () => CKEditorParams.editorObject,
  getRanges: () => util.getEditor().getSelection().getRanges(),
  getSelectedText: () => {
    return util.getEditor().getSelection().getSelectedText();
  },
  getAndDelInput: (inputValue) => {
    let ranges = util.getRanges();
    if (!ranges.length) return;
    let range = ranges[0], {startContainer, startOffset} = ranges[0], text = startContainer.getText();
    // if (!inputValue) {
    //   inputValue = startContainer.getText()[startOffset - 1];
    // }
    // if (!inputValue) return;
    let length = inputValue.length;

    // fix: 中文输入重复记录痕迹问题
    // 删除当前输入的内容
    text = text.replace(inputValue, '');
    // text = text.substr(0,startOffset - 1) + text.substr(startOffset + length - 1);
    startContainer.setText(text);

    if (range.startOffset < length) return;
    range.setStart(range.startContainer, range.startOffset - length);
    range.setEnd(range.endContainer, range.endOffset - length);
    range.select();
    return inputValue;
  },

  getContentLength: (node) => {
    if (isElement(node)) {
      return node.getChildCount();
    } else if (isText(node)) {
      return clearEmptyChar(node.getText()).length;
    } else {
      return 0;
    }
  },
  isNode,
  isText,
  isElement,
  isParagraph,
  isRoot,
  isParagraphFirst: (node) => {
    if (!isNode(node)) return false;
    if (isElement(node) && isParagraph(node)) return true;
    else {
      if (node.getIndex() !== 0) return false;
      return isParagraph(node.getParent()) || util.isParagraphFirst(node.getParent());
    }
  },
  isParagraphLast: (node) => {
    if (!isNode(node)) return false;
    if (isElement(node) && isParagraph(node)) return true;
    else {
      if (node.getIndex() !== node.getParent().getChildCount() - 1) return false;
      return isParagraph(node.getParent()) || util.isParagraphLast(node.getParent());
    }
  },
  /**
   * 对该节点的文本合并并清空空值
   * @param node
   */
  normalizeNode: (node) => {
    if (util.isNode(node)) {
      node.$.normalize();
      let parent = node.getParent();
      if (util.isNode(parent)) {
        parent.$.normalize();
        util.clearNodeEmptyChar(parent);
      } else
        util.clearNodeEmptyChar(node);
    }
  },
  /**
   * 对当前所在节点合并文本并清空空值
   */
  normalizeCurrent: () => {
    let ranges = util.getRanges();
    if (ranges.length < 1) return;
    let node = ranges[0].startContainer;
    util.normalizeNode(node);
  },

  /**
   * 清空该节点的空值
   * @param node
   */
  clearNodeEmptyChar: (node) => {
    if (isText(node)) {
      let text = node.getText(), clearText = clearEmptyChar(text);
      text !== clearText && node.setText(clearText);
    } else if (isElement(node)) {
      node.getChildren().toArray().forEach(util.clearNodeEmptyChar);
    }
  },
  clearEmptyChar,

  /**
   * 合并当前所在段落的删除标签
   */
  mergeCurrentNode: (revision) => {
    let ranges = util.getRanges();
    if (ranges.length < 1) return;
    let node = ranges[0].startContainer;
    while (isNode(node) && !isParagraph(node)) {
      node = node.getParent();
    }
    if (!isNode(node)) return;
    return RevisionItem.mergeNode(node, revision);
  },

  mergeAll: (revision) => {
    let body = util.getEditor().document.getBody();
    RevisionItem.mergeNode(body, revision);
    // body.getChildren().toArray().forEach((node) => RevisionItem.mergeNode(node, revision) );
  },
  lock: (...args) => {
    util.getEditor().undoManager.lock(...args);
  },
  unlock: () => {
    util.getEditor().undoManager.unlock();
  },
};
export default util;